<template>
    <div>
        <div style="text-align: right">
            <el-button type="success" @click="add">创建字典</el-button>
        </div>
        <div style="width: 100%; margin-top: 10px">
            <el-table
                    :data="tableData"
                    border
                    style="width: 100%;">
                <el-table-column
                        prop="value"
                        label="键值"
                        align="center"
                >
                </el-table-column>
                <el-table-column
                        prop="label"
                        label="标签名"
                        align="center"
                >
                </el-table-column>
                <el-table-column
                        prop="description"
                        label="描述"
                        align="center"
                >
                </el-table-column>
                <el-table-column
                        prop="type"
                        label="类型"
                        align="center"
                >
                </el-table-column>
                <el-table-column
                        prop="sort"
                        label="排序"
                        align="center"
                >
                </el-table-column>
                <el-table-column
                        prop="address"
                        label="操作"
                        align="center"
                >
                    <template slot-scope="scope">
                        <el-button @click="handleClick(scope.row)" type="success"  size="mini">修改</el-button>
                        <el-button type="danger" size="mini" @click="deleterow(scope.row)">删除</el-button>
                    </template>
                </el-table-column>
            </el-table>
        </div>
        <el-pagination
                style="text-align: center"
                @size-change="handleSizeChange"
                @current-change="handleCurrentChange"
                :current-page="pageNo"
                :page-sizes="[10, 20, 30, 40]"
                :page-size="pageSize"
                layout="total, sizes, prev, pager, next, jumper"
                :total="totalRecord">
        </el-pagination>
        <el-dialog title="修改内容" :visible.sync="dialogFormVisible">
            <el-form :model="form">
                <el-form-item label="键值" :label-width="formLabelWidth">
                    <el-input v-model="form.value" autocomplete="off"></el-input>
                </el-form-item>
                <el-form-item label="标签名" :label-width="formLabelWidth">
                    <el-input v-model="form.label" autocomplete="off"></el-input>
                </el-form-item>
                <el-form-item label="描述" :label-width="formLabelWidth">
                    <el-input v-model="form.description" autocomplete="off"></el-input>
                </el-form-item>
                <el-form-item label="类型" :label-width="formLabelWidth">
                    <el-input v-model="form.type" autocomplete="off"></el-input>
                </el-form-item>
                <el-form-item label="排序" :label-width="formLabelWidth">
                    <el-input v-model="form.sort" autocomplete="off"></el-input>
                </el-form-item>
            </el-form>
            <div slot="footer" class="dialog-footer">
                <el-button @click="dialogFormVisible = false">取 消</el-button>
                <el-button type="primary" v-if="showupdate" @click="goupdate">确 定</el-button>
                <el-button type="primary" v-if="showadd" @click="goadd">确 定</el-button>
            </div>
        </el-dialog>

    </div>
</template>

<script>
    export default {
        name: "index",
        data(){
            return{
                pageNo:1,
                pageSize:10,
                totalRecord:0,
                showupdate: true,
                showadd: true,
                id:'',
                deleteid:'',
                tableData: [],
                dialogFormVisible: false,
                form: {
                    value: '',
                    label: '',
                    description: '',
                    type: '',
                    sort: ''
                },
                formLabelWidth: '120px'
            }
        },
        methods:{
            async deleterow(row){
                this.deleteid = row.id
                this.$confirm('此操作将永久删除该文件, 是否继续?', '提示', {
                    confirmButtonText: '确定',
                    cancelButtonText: '取消',
                    type: 'warning'
                }).then(async () => {
                    let res = await this.$http('deletelist', {
                        id: this.deleteid
                    });
                    this.init()
                    console.log(res,'123')
                    this.$message({
                        type: 'success',
                        message: '删除成功!'
                    });
                }).catch(() => {
                    this.$message({
                        type: 'info',
                        message: '已取消删除'
                    });
                });
            },
            async goadd(){
                let res = await this.$http('createlist',{
                    value: this.form.value,
                    label: this.form.label,
                    description: this.form.description,
                    sort:this.form.sort,
                    type:this.form.type
                })
                this.dialogFormVisible = false;
                this.init();
            },
            async goupdate (){
                let res = await this.$http('createlist',{
                    id: this.id,
                    value: this.form.value,
                    label: this.form.label,
                    description: this.form.description,
                    sort:this.form.sort,
                    type:this.form.type
                })
                this.dialogFormVisible = false;
                this.init();
            },
            async add(){
                this.showupdate = false
                this.showadd = true
                this.dialogFormVisible = true;
                this.form = {}
            },
            async init (){
                let res = await this.$http('dictlist',{
                    pageNo:this.pageNo,
                    pageSize:this.pageSize,
                });
                this.totalRecord = res.totalRecord
                this.tableData = res.results
            },

            async handleClick(row) {
                this.showadd = false
                this.showupdate = true
                this.id = row.id
                this.form = row
                this.dialogFormVisible = true;
            },
            handleSizeChange(val) {
                console.log(`每页 ${val} 条`);
                this.pageSize = val
                this.init()
            },
            handleCurrentChange(val) {
                console.log(`当前页: ${val}`);
                this.pageNo = val
                this.init()
            }
        },
        mounted () {
            this.init();
        },
    }
</script>

<style scoped>

</style>
